package com.purong.purong.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.PermissionConstant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.activity.CollectActivity;
import com.purong.purong.activity.HelpActivity;
import com.purong.purong.activity.InfoCenterActivity;
import com.purong.purong.activity.LoanIntentActivity;
import com.purong.purong.activity.BankLoanRecordActivity;
import com.purong.purong.activity.LoginActivity;
import com.purong.purong.activity.PersionCheckingActivity;
import com.purong.purong.activity.PersionInfoActivity;
import com.purong.purong.activity.ScheduleQueryActivity;
import com.purong.purong.activity.SettingActivity;
import com.purong.purong.activity.SmallLoanRecordActivity;
import com.purong.purong.activity.UserInfoActivity;
import com.purong.purong.bean.ConfigBean;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.GradationScrollView;
import com.purong.purong.widget.RadiusImageView;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;
import com.yaoming.umengshare.SharePlatform;
import com.yaoming.umengshare.UmShare;
import com.yaoming.umengshare.listener.ShareResultListener;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/1.
 */
public class MineFragment extends BasePermFragment implements GradationScrollView.ScrollViewListener{
    private GradationScrollView mScrollView;
    private TextView titleName,noLogin,userPhone,userNick;
    private RadiusImageView avatar;
    private ImageView statusBar;
    private LinearLayout titleBar;
    private RelativeLayout topLay;
    private int titleBarRange;
    @Override
    public int getRootViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (PuRongApp.getLoginInfo(getActivity())!=null){
            LoginBean bean=PuRongApp.getLoginInfo(getActivity());
            if (bean.getNick()!=null&&!bean.getNick().equals("")){
                userNick.setText(bean.getNick());
            }else {
                userNick.setText("设置昵称");
            }
            String phone=bean.getPhone();
            if (phone!=null){
                if (phone.length()>=7){
                    userPhone.setText(phone.substring(0,3)+"****"+phone.substring(7,phone.length()));
                }else {
                    userPhone.setText(phone);
                }
            }else {
                userPhone.setText("");
            }
            userNick.setVisibility(View.VISIBLE);
            userPhone.setVisibility(View.VISIBLE);
            noLogin.setVisibility(View.GONE);
            GlideUtil.getInstance().loadAvatar2(getActivity(),bean.getAvatar(),avatar);
        }else {
            userNick.setText("");
            userPhone.setText("");
            userNick.setVisibility(View.GONE);
            userPhone.setVisibility(View.GONE);
            noLogin.setVisibility(View.VISIBLE);
            avatar.setImageResource(R.drawable.def_avatar);
        }
    }

    @Override
    public void initView(View v) {
        titleBarRange=CommonUtil.dip2px(getActivity(),100);
        avatar=(RadiusImageView)v.findViewById(R.id.avatar);
        noLogin=(TextView)v.findViewById(R.id.no_login);
        userPhone=(TextView)v.findViewById(R.id.user_phone);
        userNick=(TextView)v.findViewById(R.id.user_nick);
        topLay=(RelativeLayout)v.findViewById(R.id.top_lay);
        mScrollView = (GradationScrollView) v.findViewById(R.id.scrollview);
        mScrollView.setScrollViewListener(this);
        titleName = (TextView) v.findViewById(R.id.title_name);
        titleBar = (LinearLayout) v.findViewById(R.id.title_bar);
        statusBar = (ImageView) v.findViewById(R.id.status_bar);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
            statusBar.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
            topLay.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,CommonUtil.dip2px(getActivity(),177)-CommonUtil.getStatusBarHeight(getActivity())));

        }else {
            statusBar.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.getStatusBarHeight(getActivity())));
             topLay.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,CommonUtil.dip2px(getActivity(),177)));

        }
        v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),InfoCenterActivity.class);
                intent.putExtra("PAGE",InfoCenterActivity.PAGE_NOTICE);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.user_info_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), UserInfoActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.schedule_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), ScheduleQueryActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }

            }
        });

        v.findViewById(R.id.small_loan_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), SmallLoanRecordActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.bank_loan_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), BankLoanRecordActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.loan_intention).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), LoanIntentActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), CollectActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }

            }
        });
        v.findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 23) {
                    checkPermission();
                } else {
                    getAppShare();
                }
            }
        });
        v.findViewById(R.id.help).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), HelpActivity.class);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.persion_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), PersionInfoActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.persion_checking).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), PersionCheckingActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        v.findViewById(R.id.notice_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), PersionInfoActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            titleName.setTextColor(Color.argb((int) 0, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));
            statusBar.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));
        } else if (y > 0 && y <= titleBarRange) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / titleBarRange;
            float alpha = (255 * scale);
            titleName.setTextColor(Color.argb((int) alpha, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) alpha, 71, 158, 248));
            statusBar.setBackgroundColor(Color.argb((int) alpha, 71, 158, 248));
        } else {    //滑动到banner下面设置普通颜色
            titleName.setTextColor(Color.argb((int) 255, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) 255, 71, 158, 248));
            statusBar.setBackgroundColor(Color.argb((int) 255, 71, 158, 248));

        }
    }
    private void share(String url){
        UmShare share = new UmShare(getActivity(), new ShareResultListener() {
            @Override
            public void onSuccess(String platform) {
            }

            @Override
            public void onCancel(String platform) {
            }

            @Override
            public void onError(String platform, Throwable t) {
                showToast(t.toString());
            }
        });
        share.setImage(R.mipmap.app_icon);
        share.setText("普融，中国中小企业普惠金融O2O模式领导者。");
        share.setTitle("普融—中国中小企业普惠金融O2O模式领导者");
        if (url!=null&&url.startsWith("http")){
            share.setTargetUrl(url);
        }else {
            share.setTargetUrl("http://www.purong.com/");
        }
        share.setPlatform(new SharePlatform(SHARE_MEDIA.WEIXIN),new SharePlatform(SHARE_MEDIA.WEIXIN_CIRCLE),new SharePlatform(SHARE_MEDIA.QQ));
        share.share();
    }

    private void getAppShare(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("config_code", "AppShareUrl");
        request(Contant.CONFIG, params, true,true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.CONFIG)){
            if (responseInfo.isRequestOk){
                try{
                    ConfigBean bean= JSON.parseObject(responseInfo.content,ConfigBean.class);
                    if (bean!=null&&bean.getConfig()!=null&&bean.getConfig().getAppShareUrl()!=null){
                        share(bean.getConfig().getAppShareUrl());
                    }
                }catch (Exception e){

                }
                Log.e("zwj",responseInfo.content);
            }
        }
    }

    @Override
    protected String[] getPermissions() {
        return PermissionConstant.PERMS_STORAGE;
    }

    @Override
    protected void doPermissinosSuc() {
        getAppShare();
    }
}
